// pages/edit/edit.js
const app = getApp()
const fs = wx.getFileSystemManager() // 文件管理器API
const wxAudio = wx.createInnerAudioContext({}) // 音频API
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    selfData: [],
    chineseValue: "",
    englishValue: "",
    isShowEmpty: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      selfData: app.globalData.selfData,
    })
    this.setData({
      isShowEmpty: this.data.selfData.length == 0
    })
  },
  clickPlay(event){
    wx.showLoading({
      title: '数据请求中',
      image:"../../image/icon_doctor.png"
    })
    var item=event.currentTarget.dataset.item;
    var text=item.chinese+","+item.chinese+","+item.english;
    const target = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.mp3`
    var url = 'https://yuanxiapi.cn/api/voice/?text=' + text+'&spd='+app.globalData.audioSpeed;
    wx.request({
      url: url,
      method: 'GET',
      responseType: 'arraybuffer',
      success(result) {
        console.log(result)
        if (result.statusCode == 200 || result.statusCode == 201) {
          if(result.data!=null){
            fs.writeFile({
              filePath: target,
              data: result.data,
              encoding: 'binary',
              success: (res) => {   
                wxAudio.src = target
              },
              fail:()=>{
                wx.hideLoading()
                wx.showModal({
                  title: '播放失败,请重试',
                  content: result,
                })
              }
            })
            // onCanplay监听音频进入可以播放状态的事件
            wxAudio.onCanplay(() => {
              wx.showLoading({
                title: '播放中',
              })
              wxAudio.play()
            }),
            wxAudio.onEnded(() =>{
              wx.hideLoading()
            })
            wxAudio.onError(() =>{
              wx.hideLoading()
              wx.showModal({
                title: '播放失败,请重试',
                content: result,
              })
            })
          }else{
            wx.hideLoading()
            wx.showModal({
              title: '播放失败,请重试',
              content: result,
            })
          }
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '播放失败,请重试',
            content: result,
          })
        }
      },fail(){
        wx.hideLoading()
      }
    })
  },
  delete(event) {
    //删除数据
    this.data.selfData.splice(event.currentTarget.dataset.index, 1);
    this.setData({
      selfData: this.data.selfData,
    })
    this.setData({
      isShowEmpty: this.data.selfData.length == 0
    })
    //保存数据
    wx.setStorage({
      key: "selfData",
      data: this.data.selfData
    })
    //刷新globalData
    app.globalData.selfData = this.data.selfData
  },
  clickAdd() {
    this.setData({
      show: true,
      chineseValue: "",
      englishValue: ""
    })
  },
  onClosePop() {
    this.setData({
      show: false
    })
  },
  // 确认
  clickSure() {
    if (this.data.chineseValue == "" || this.data.englishValue == "") {
      wx.showToast({
        title: '请填入数据',
      })
      return;
    }
    var newData = {
      "chinese": "",
      "english": ""
    };
    newData.chinese = this.data.chineseValue;
    newData.english = this.data.englishValue;
    this.data.selfData = this.data.selfData.concat(newData);
    this.setData({
      show: false,
      selfData: this.data.selfData,
    })
    this.setData({
      isShowEmpty: this.data.selfData.length == 0
    })
    //保存数据
    wx.setStorage({
      key: "selfData",
      data: this.data.selfData
    })
    //刷新globalData
    app.globalData.selfData = this.data.selfData
  },
  onChineseChange(event) {
    this.setData({
      chineseValue: event.detail
    })
  },
  onEnglishChange(event) {
    this.setData({
      englishValue: event.detail
    })
  }
})